package com.millstein.controller;

import com.millstein.enums.coupon.CategoryEnum;
import com.millstein.request.NewUserCouponRequest;
import com.millstein.service.CouponService;
import com.millstein.util.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author millstein
 * @since 2021-09-14
 */
@Api("优惠券控制器")
@RestController
@RequestMapping("/api/coupon/v1")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @GetMapping("page_coupon")
    @ApiOperation("分页查询优惠券信息")
    public JsonData pageCoupon(@ApiParam("当前页") @RequestParam(value = "page", defaultValue = "1") int page,
                               @ApiParam("每页记录数") @RequestParam(value = "size", defaultValue = "10") int size) {
        return JsonData.buildSuccess(couponService.pageCoupon(page, size));
    }

    @PostMapping("/promotion/{coupon_id}")
    @ApiOperation("领用促销优惠券")
    public JsonData addPromotionCoupon(@ApiParam("优惠券id") @PathVariable("coupon_id") Long id) {
        couponService.addCoupon(id, CategoryEnum.PROMOTION);
        return JsonData.buildSuccess();
    }

    // 这个接口只有注册成功时才会调用，这个时候不会有用户的登录信息，所以这个接口不能被登录拦截器所拦截
    @PostMapping("/new_user")
    @ApiOperation("rpc-发放新用户优惠券")
    public JsonData addNewUserCoupon(@ApiParam("用户信息") @RequestBody NewUserCouponRequest newUserCouponRequest) {
        couponService.addNewUserCoupon(newUserCouponRequest);
        return JsonData.buildSuccess();
    }
}

